{ INFORMATICS.MCCME.RU
  Problem statement: http://informatics.mccme.ru/moodle/mod/statements/view3.php?chapterid=1166
  Verdict: Accepted
}
uses 
	SysUtils, Math;
 
var  	
	i,n,kk:longint;
	a:array[0..100010] of longint;
 
procedure siftdown(i:longint);
 
var 
  j,t:longint;
 
begin
while 2*i<=n do begin
  j:=i;
	if a[2*i]>a[j] then j:=2*i;
	if (2*i+1<=n) and (a[2*i+1]>a[j]) then j:=2*i+1;
  if i<j then begin
    t:=a[i];
    a[i]:=a[j];
    a[j]:=t;
    i:=j;
	end else break;
end;
write(i,' ');
end;
 
function ex_max:longint;
begin
result:=a[1];
a[1]:=a[n];
dec(n);
siftdown(1);
writeln(result);
end;
 
begin
reset(input,'input.txt');
rewrite(output,'output.txt');
read(n);
for i:=1 to n do
	read(a[i]);
kk:=n-1;
for i:=1 to kk do
	ex_max;
end.